package com.paul.sys.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CommTools {

	public static String [] prov_Code={"11","12","13","14","15","21","22","23","31","32","33","34","35","36","37","41","42","43","44","45","46","50","51","52","53","54","61","62","63","64","65"};
	public static String [] prov_Name={"京","津","冀","晋","蒙","辽","吉","黑","沪","苏","浙","皖","闽","赣","鲁","豫","鄂","湘","粤","桂","琼","渝","川","贵","云","藏","陕","甘","青","宁","新"};
	public static String code_To_name(String code){
		String name="";
		int len=prov_Code.length;
		for(int i=0;i<len;i++){
			if(prov_Code[i].equals(code)){
				name=prov_Name[i];
			}
		}
		return name;
	} 
	
	public static String name_To_code(String name){
		String code="";
		int len=prov_Name.length;
		for(int i=0;i<len;i++){
			if(prov_Name[i].equals(name)){
				code=prov_Code[i];
			}
		}
		return code;
	}
	
	public static final String getVehNameByCode(String code) {
		if (code==null || code.length()<5) {
			return null;
		}
		String codeHead = code.substring(0,2);
		String nameHead = code_To_name(codeHead);
		return nameHead + code.substring(2);
	}
	
	public static final String getVehCodeByName(String name) {
		if (name==null || name.length()<5) {
			return null;
		}
		String nameHead = name.substring(0,1);
		String codeHead = name_To_code(nameHead);
		return codeHead + name.substring(1);
	}
	
	public static final String DEFAULT_DATEFORAMT_STRING = "yyyy-MM-dd HH:mm:ss";
	public static final String DEFAULT_ORACLE_FORMATSTRING = "YYYY-MM-DD HH24:MI:SS";
	private static ThreadLocal<DateFormat> DEFAULT_FORMAT_LOCAL = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(DEFAULT_DATEFORAMT_STRING);
		}
	};
	
	public static final String defaultFormat(Date date) {
		return DEFAULT_FORMAT_LOCAL.get().format(date);
	}
	public static final Date defaultParse(String dateStr) throws ParseException {
		return DEFAULT_FORMAT_LOCAL.get().parse(dateStr);
	}
	
	public static final String SIMPLE_DATEFORAMT_STRING = "yyyyMMddHHmmss";
	public static final String SIMPLE_ORACLE_FORMATSTRING = "YYYYMMDDHH24MISS";
	private static ThreadLocal<DateFormat> SIMPLE_FORMAT_LOCAL = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(SIMPLE_DATEFORAMT_STRING);
		}
	};
	public static final String simpleFormat(Date date) {
		return SIMPLE_FORMAT_LOCAL.get().format(date);
	}
	public static final Date simpleParse(String dateStr) throws ParseException {
		return SIMPLE_FORMAT_LOCAL.get().parse(dateStr);
	}
	
	public static final String DEFAULT_NOTIME_DATEFORAMT_STRING = "yyyy-MM-dd";
	public static final String DEFAULT_NOTIME_ORACLE_FORMATSTRING = "YYYY-MM-DD";
	private static ThreadLocal<DateFormat> DEFAULT_NOTIME_FORMAT_LOCAL = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(DEFAULT_NOTIME_DATEFORAMT_STRING);
		}
	};
	public static final String defaultNotimeFormat(Date date) {
		return DEFAULT_NOTIME_FORMAT_LOCAL.get().format(date);
	}
	public static final Date defaultNotimeParse(String dateStr) throws ParseException {
		return DEFAULT_NOTIME_FORMAT_LOCAL.get().parse(dateStr);
	}
	
	public static final String SIMPLE_NOTIME_DATEFORAMT_STRING = "yyyyMMdd";
	public static final String SIMPLE_NOTIME_ORACLE_FORMATSTRING = "YYYYMMDD";
	private static ThreadLocal<DateFormat> SIMPLE_NOTIME_FORMAT_LOCAL = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(SIMPLE_NOTIME_DATEFORAMT_STRING);
		}
	};
	public static final String simpleNotimeFormat(Date date) {
		return SIMPLE_NOTIME_FORMAT_LOCAL.get().format(date);
	}
	public static final Date simpleNotimeParse(String dateStr) throws ParseException {
		return SIMPLE_NOTIME_FORMAT_LOCAL.get().parse(dateStr);
	}
	
	public static final String DEFAULT_TIME_DATEFORAMT_STRING = "HH:mm:ss";
	public static final String DEFAULT_TIME_ORACLE_FORMATSTRING = "HH24:MI:SS";
	private static ThreadLocal<DateFormat> DEFAULT_TIME_FORMAT_LOCAL = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(DEFAULT_TIME_DATEFORAMT_STRING);
		}
	};
	public static final String defaultTimeFormat(Date date) {
		return DEFAULT_TIME_FORMAT_LOCAL.get().format(date);
	}
	public static final Date defaultTimeParse(String dateStr) throws ParseException {
		return DEFAULT_TIME_FORMAT_LOCAL.get().parse(dateStr);
	}
	
	public static final String SIMPLE_TIME_DATEFORAMT_STRING = "HHmmss";
	public static final String SIMPLE_TIME_ORACLE_FORMATSTRING = "HH24MISS";
	private static ThreadLocal<DateFormat> SIMPLE_TIME__FORMAT_LOCAL = new ThreadLocal<DateFormat>() {
		protected synchronized DateFormat initialValue() {
			return new SimpleDateFormat(SIMPLE_TIME_DATEFORAMT_STRING);
		}
	};
	public static final String simpleTimeFormat(Date date) {
		return SIMPLE_TIME__FORMAT_LOCAL.get().format(date);
	}
	public static final Date simpleTimeParse(String dateStr) throws ParseException {
		return SIMPLE_TIME__FORMAT_LOCAL.get().parse(dateStr);
	}
	
	
	

	/**
	 * 设置查询参数
	 * @param param
	 * @param key
	 * @param value
	 * @return
	 */
	public static final Map<String, String> getQueryParam(Map<String, String> param,
			String key, String value) {
		if(key == null)
			return new HashMap<String, String>(0);
		
		if (param == null) {
			param = new HashMap<String, String>();
		}else{
			param.clear();
		}
		param.put(key, value);
		return param;
	}
	
	/**
	 * 设置查询参数
	 * @param param
	 * @param key
	 * @param value
	 * @return
	 */
	public static final Map<String, String> getQueryParam(Map<String, String> param,
			String []key, String []value) {
		if(key == null || value == null || key.length != value.length) 
			return new HashMap<String, String>(0);
			
		if (param == null) {
			param = new HashMap<String, String>();
		}else{
			param.clear();
		}
		for(int i=0,il = key.length; i<il; i++){
			param.put(key[i], value[i]);
		}
		return param;
	}
	// 
}
